package view.list;

import event.Broadcaster;
import event.ChangeDirectoryEvent;
import event.ChangeDirectoryListener;
import filesystem.MyFile;
import filesystem.local.MyLocalFileSystem;

import javax.swing.*;


import java.awt.*;
import java.awt.event.*;


public abstract class ListView extends JPanel {

  protected JScrollPane scroll = null;
  protected JPanel topPanel=null;
  protected JComboBox rootsCombo=new JComboBox();
  protected ComboBoxCellRenderer comboRenderer=new ComboBoxCellRenderer();
  private boolean shouldFireComboEvent=true;
    MyLocalFileSystem fileSystem = new MyLocalFileSystem();

    public ListView() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }  	
  }

  private void jbInit() throws Exception {
  	topPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
    topPanel.add(rootsCombo);
//    topPanel.add(statusLabel);
    setupRootsCombo();        
//    Options.setActivePanel(this);
//    statusLabel.setText(fileSystem.getStartDirectory().getAbsolutePath());
//    statusLabel.setForeground(UIManager.getColor("Label.disabledForeground"));
    Broadcaster.addChangeDirectoryListener(new ChangeDirectoryListener(){
      public void changeDirectory(ChangeDirectoryEvent e){
    	  if(getCurrentWorkingDirectory()!=null)
    	  {
    		  MyFile f= getCurrentWorkingDirectory().getRootDriveFile();
    		  for(int i=0;i<rootsCombo.getItemCount();i++)
    		  {
    			ComboBoxCellObject obj=  (ComboBoxCellObject)rootsCombo.getItemAt(i);
    			if(obj.getFile().equals(f))
    			{
    				shouldFireComboEvent=false;
    				//rootsCombo.setSelectedIndex(i);
    			}
    		  }
    	  }
      }
    });
    
  }

  public void requestFocus(){
    //super.requestFocus();
    //getViewComponent().requestFocus();
  	getViewComponent().requestFocusInWindow();
  }
  

  public abstract MyFile getSelectedFile();

  public abstract MyFile[] getSelectedFiles();

  public abstract MyFile getCurrentWorkingDirectory();
  

    private void setupRootsCombo(){
    	rootsCombo.setRenderer(comboRenderer);
    	rootsCombo.setEditable(false);    	
    	MyFile[] roots= fileSystem.listRoots();
    	if(roots!=null){
    		for(int i=0;i<roots.length;i++){
    			rootsCombo.addItem(new ComboBoxCellObject(roots[i]));
    			if(fileSystem !=null){
    				@SuppressWarnings("unused")
                    MyFile startDir= fileSystem.getStartDirectory();
//    				MyFile curDir=getCurrentWorkingDirectory();
    				if(fileSystem.getStartDirectory().getRootDriveFile().equals(roots[i])){
    					rootsCombo.setSelectedIndex(i);
    				}
    			}
    		}
    	}    	
    	rootsCombo.addItemListener(new ItemListener(){
    		public void itemStateChanged(ItemEvent e){
    			if(e.getStateChange()==ItemEvent.DESELECTED){
    				return;
    			}
    			if(!shouldFireComboEvent)
    			{
    				shouldFireComboEvent=true;
    				//return;
    			}
    			changeDirectory(((ComboBoxCellObject)rootsCombo.getSelectedItem()).getFile());
    		}
    	});
    }
    

    protected abstract void changeDirectory(MyFile file);


    protected abstract JComponent getViewComponent();  
}
